﻿using System;
using Fly.Data;
using FlyWebFrame.Model;

namespace FlyWebFrame.Action
{
    public abstract class homeaction : Fly.Web.Action.Handler
    {
        public string page_title;
        public string content_left = "/html/home/left.htm";
        public string content_right;
        public string client_script;
        public dynamic nav;
        public dynamic subcats;
        public string big_title;
        public string current_title;
        public string template;
        public string detail;
        public dynamic newslist;
        public string lang = "vi";
        public string other_lang;
        public string lang_name;
        public string current_link;
        public string banner;
        public string contact_name;
        public string home_name;

        public dynamic others;
        public string date;
        public string news_title;
        public string news_detail;
        public string news_image;
        public string en_other_title;
        public string vi_other_title;
        public string type_link;

        public string meta_title;
        public string meta_description;
        public string keywords;

        public void Handle()
        {
            SetView("/html/home/main.htm");
            page_title = "LEAD Consulting - Make it happen";
            meta_title = page_title;
            lang = GetParameter(0);
            if (lang == "en")
            {
                lang = "en";
                lang_name = "Tiếng Việt";
                other_lang = "vi";
                contact_name = "Contact";
                home_name = "Home";
            }
            else {
                lang = "vi";
                lang_name = "English";
                other_lang = "en";
                contact_name = "Liên hệ";
                home_name = "Trang chủ";
            }
            // get menu top
            Categories category = new Categories();
            nav = category.GetChild(0).Data;
            if (lang == "vi" && nav != null) {
                for (int i = 0; i < nav.Length; i++) {
                    nav[i].name = nav[i].viName;
                }
            }
            dynamic subnav;
            client_script += "var lang = '"+ lang+"';var subnavs = new Array();";
            if (nav != null)
            {
                for (int i = 0; i < nav.Length; i++)
                {
                    subnav = category.GetChild(nav[i].id).Data;
                    if (subnav != null)
                    {
                        if (lang != "en" ) {
                            for (int j = 0; j < subnav.Length; j++) {
                                subnav[j].name = subnav[j].viName;
                            }
                        }
                        client_script += "subnavs.push('" + Fly.Util.Serializer.Json.Encode(subnav) + "');";
                    }
                }
            }
            this.Treat();
        }

        public abstract void Treat();

        public string ToVietnamDate(string dayOfWeek)
        {
            switch (dayOfWeek)
            {
                case "Monday":
                    return "Thứ hai";
                case "Tuesday":
                    return "Thứ ba";
                case "Wednesday":
                    return "Thứ tư";
                case "Thursday":
                    return "Thứ năm";
                case "Friday":
                    return "Thứ sáu";
                case "Saturday":
                    return "Thứ bảy";
                default:
                    return "Chủ nhật";
            }
        }

        public void renderLeftNav(int catId)
        {
            Categories cat = new Categories();
            cat.id = catId;
            cat.Instance();
            big_title = cat.name;
            meta_title = page_title + " - " + big_title;
            banner = cat.image;
            if (cat.parentId != 0)
            {
                current_title = cat.name;
                int parentId = cat.parentId;
                Categories parent = new Categories();
                parent.id = parentId;
                parent.Instance();
                big_title = parent.name;
               
                banner = parent.image;
                subcats = parent.GetChild(parentId).Data;
                if (lang != "en" && subcats != null)
                {
                    big_title = parent.viName;
                    current_title = cat.viName;
                    for (int i = 0; i < subcats.Length; i++)
                    {
                        subcats[i].name = subcats[i].viName;
                    }
                }
                client_script += "$('#leftnav" + catId + "').addClass('active');";
                client_script += "$('#subnav" + catId + "').addClass('active');";
                client_script += "$('#tab" + parentId + "').addClass('active');";
                client_script += "$('#header" + parentId + "').addClass('active');";
                meta_title = page_title + " - " + current_title;
            }
            else {
                if (lang != "en") {
                    big_title = cat.viName;
                }
                subcats = cat.GetChild(cat.id).Data;
                if (subcats != null)
                {
                    current_title = subcats[0].name;
                    if (lang != "en")
                    {
                        current_title = subcats[0].viName;
                        for (int i = 0; i < subcats.Length; i++)
                        {
                            subcats[i].name = subcats[i].viName;
                        }
                    }
                    //if (catId == 9)  
                    //{
                    //    client_script += "$('#leftnav33').addClass('active');";
                    //    client_script += "$('#subnav33').addClass('active');";
                    //}
                    //else
                    //{
                        client_script += "$('#leftnav" + subcats[0].id + "').addClass('active');";
                        client_script += "$('#subnav" + subcats[0].id + "').addClass('active');";
                    //}
                }
                client_script += "$('#tab" + cat.id + "').addClass('active');";
                client_script += "$('#header" + cat.id + "').addClass('active');";
            }
            template = "/html/home/" + cat.template;
            meta_title = page_title + " - " + current_title;
        }

        public void renderFirstNews(int catId){
            News news = new News();
            dynamic firstnews = news.GetByCat(catId,0,1).Data;
            if (firstnews != null)
            {
                detail = firstnews[0].detail;
                if (lang != "en")
                {
                    detail = firstnews[0].viDetail;
                }
                meta_title = page_title + " - " + firstnews[0].title;
                string tmp = detail;
                tmp = tmp.ToString().Replace("\"", "");
                tmp = tmp.Replace("<", "");
                tmp = tmp.Replace(">", "");
                tmp = tmp.Replace("'", ""); 
                meta_description = Fly.Util.Text.Shorten(tmp, 300);
            }
            else {
                client_script += "$('#none_"+lang+"').removeClass('hidden')";
            }
        }

        public void renderListNews(int[] catIds, int page, int size)
        {
            for (int i = 0; i < catIds.Length; i++)
            {
                renderListNews(catIds[i], page, size);
            }
        }

        public void renderListNews(int catId, int page, int size)
        {
            News news = new News();
            newslist = news.GetByCat(catId, page, size).Data;
            if (newslist != null) {
                if (lang != "en")
                {
                    for (int i = 0; i < newslist.Length; i++)
                    {
                        newslist[i].title = newslist[i].viTitle;
                        newslist[i].description = newslist[i].viDescription;
                        newslist[i].detail = newslist[i].viDetail;

                        if (newslist[i].type == 4) {
                            client_script += "$('#attachment_vi" + newslist[i].id + "').removeClass('hidden');";
                            client_script += "$('#attachment_en" + newslist[i].id + "').addClass('hidden');";
                        }
                    }
                }
                for (int i = 0; i < newslist.Length; i++)
                {
                    if (newslist[i].type == 2 && newslist[i].title.IndexOf("<br") != -1)
                    {
                        client_script += "$('#course" + newslist[i].id + "').css('padding','17px 5px');";
                    }
                    if (newslist[i].type == 4 && i>0)
                    {
                        if (newslist[i].image == newslist[i - 1].image)
                        {
                            client_script += "$('#attachment_vi" + newslist[i-1].id + "').addClass('hidden');";
                            client_script += "$('#attachment_en" + newslist[i-1].id + "').addClass('hidden');";
                            client_script += "$('#upcomming" + newslist[i].id + "').addClass('hidden');";
                            client_script += "$('#course_row" + newslist[i].id + "').css('padding','0 5px');";
                        }
                    }
                }
                meta_title = page_title + " - " + newslist[0].title;
            }
        }

        public void renderNews(int newsId) {
            News news = new News();
            news.id = newsId;

            if (news.Instance().Status && news.status == 1)
            {
                news_title = news.title;
                news_detail = news.detail;
                news_image = news.image;
                if (lang != "en")
                {
                    news_title = news.viTitle;
                    news_detail = news.viDetail;
                }
                date = this.ToVietnamDate(news.since.DayOfWeek.ToString()) + ", " + news.since.Day.ToString() + "/" + news.since.Month.ToString() + "/" + news.since.Year.ToString() + ", " + news.since.Hour.ToString() + ":" + news.since.Minute.ToString() + " GMT+7";

                // get similar news
                renderLeftNav(news.catId);
                content_right = "/html/home/newsview.htm";
                others = news.GetOther(news.catId, news.id).Data;
                if (others != null)
                {
                    vi_other_title = "";
                    if (news.type == 1)
                    {
                        en_other_title = "Other news in this topic";
                        type_link = "newsdetail";
                    }
                    else {
                        en_other_title = "Other courses";
                        type_link = "coursedetail";
                    }

                    lang = GetParameter(0);
                    if (lang != "en")
                    {
                        en_other_title = "";
                        if (news.type == 1)
                        {
                            vi_other_title = "Các tin khác";
                        }
                        else {
                            vi_other_title = "Các khóa học khác";
                        }
                        for (int i = 0; i < others.Length; i++) {
                            others[i].title = others[i].viTitle;
                        }
                    }

                    for (int i = 0; i < others.Length; i++)
                    {
                        others[i].Date = others[i].since.Day.ToString() + "/" + others[i].since.Month.ToString() + "/" + others[i].since.Year.ToString();
                    }
                }
                meta_title = page_title + " - " + news_title;
                string tmp = news_detail;
                tmp = tmp.ToString().Replace("\"", "");
                tmp = tmp.Replace("<", "");
                tmp = tmp.Replace(">", "");
                tmp = tmp.Replace("'", "");
                meta_description = Fly.Util.Text.Shorten(tmp, 300);
            }
        }
    }
}